package com.system.manager.modules.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.system.manager.modules.risk.RiskErrorCode;
import com.system.manager.modules.risk.controller.TbFileController;
import com.system.manager.modules.risk.dto.FileStationVo;
import com.system.manager.modules.risk.entity.StaffStation;
import com.system.manager.modules.risk.entity.TbFile;
import com.system.manager.modules.risk.entity.TbFileStation;
import com.system.manager.modules.risk.exception.riskException;
import com.system.manager.modules.risk.mapper.TbFileStationMapper;
import com.system.manager.modules.risk.service.IStaffStationService;
import com.system.manager.modules.risk.service.ITbFileService;
import com.system.manager.modules.risk.service.ITbFileStaffstationService;
import com.system.manager.modules.risk.service.ITbFileStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxp
 * @since 2021-11-23
 */
@Service
public class TbFileStationServiceImpl extends ServiceImpl<TbFileStationMapper, TbFileStation> implements ITbFileStationService {
    @Autowired
    private TbFileStationMapper tbFileStationMapper;
    @Autowired
    private TbFileController tbFileController;

    @Autowired
    private IStaffStationService iStaffStationService;
    @Autowired
    private ITbFileService iTbFileService;

    @Autowired
    private ITbFileStaffstationService iTbFileStaffstationService;
    //将推送的文件发送给部门
    @Override
    public void insertFileStation(FileStationVo fileStationVo) {
        Integer fileId = fileStationVo.getFileId();
        Integer stationId = fileStationVo.getStationId();
        //去重
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("file_id",fileId);
        wrapper.eq("station_id",stationId);
        TbFileStation fileStation1 = tbFileStationMapper.selectOne(wrapper);
        if(fileStation1 != null){
            throw new riskException(RiskErrorCode.IS_ALREADY_STATION_FILE);
        }
        Date date = new Date();
        TbFileStation fileStation = new TbFileStation();
        fileStation.setFileId(fileId);
        fileStation.setStationId(stationId);
        fileStation.setCreateTime(date);
        tbFileController.updateState(fileId);
        tbFileStationMapper.insert(fileStation);

        //将文件任务分配给该部门的用户
        List<StaffStation> staffStationList = iStaffStationService.listFindByStationId(stationId);
        TbFile file = iTbFileService.getById(fileId);
        iTbFileStaffstationService.insertFileStaffStaion(staffStationList,file);

    }

    //根据部门查找所有的文件
    @Override
    public List<Integer> selectByStationId(Integer stationId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("station_id",stationId);
        List<TbFileStation> list = tbFileStationMapper.selectList(wrapper);
        List<Integer> result = new ArrayList<>();
        for (TbFileStation o : list) {
            result.add(o.getFileId());
        }
        return result;
    }
}
